//
//  API.m
//  iWeddingDirectory
//
//  Created by Tung Ngo on 5/19/12.
//  Copyright (c) 2012 Home. All rights reserved.
//

#import "API.h"
#import "JSON.h"

#define kBaseUrl    @"http://iweddingdirectory.com.au/iwed/?json="

@implementation API

@synthesize method;
@synthesize receivedData;

- (void)dealloc
{
    [receivedData release];
    [super dealloc];
}

+ (API *)sharedInstance
{
    static API *sharedInstance = nil;
    if (sharedInstance == nil) {
        sharedInstance = [[API alloc] init];
    }
    return sharedInstance;
}

- (void)getCategories
{
    method = API_GET_CATEGORIES;
    //NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kBaseUrl, @"get_category_index/?dev=1"]];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", @"http://iweddingdirectory.com.au/iwed/api/get_category_index/?dev=1"]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)getSubCategories
{
    method = API_GET_SUB_CATEGORIES;
    //NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kBaseUrl, @"get_category_index/?dev=1"]];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", @"http://iweddingdirectory.com.au/iwed/api/get_category_index/?dev=1"]];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)getPostsInCategoryWithId:(int)categoryId
{
    method = API_GET_POSTS;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@&category_id=%d", kBaseUrl, @"get_category_posts", categoryId]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)getPostWithId:(int)postId
{
    method = API_GET_POST;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://iweddingdirectory.com.au/iwed/api/custom/get_business_info/?dev=1&id=%d", postId]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)searchForText:(NSString *)searchText
{
    method = API_SEARCH;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@get_search_results&search=%@", kBaseUrl, searchText]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)loginWithUsername:(NSString *)username andPassword:(NSString *)password
{
    method = API_LOGIN;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@generate_auth_cookie&username=%@&password=%@", kBaseUrl, username, password]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)registerWithUsername:(NSString *)username email:(NSString *)email password:(NSString *)password website:(NSString *)website message:(NSString *)message mobile:(NSString *)mobile
{
    method = API_REGISTER;
    NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@register_user&name=%@&email=%@&password=%@&website=%@&message=%@&mobile=%@", kBaseUrl, username, email, password, website, message, mobile] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];                                   
}

- (void)forgotPasswordWithEmail:(NSString *)email
{
    method = API_FORGOT_PASSWORD;
    NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@reset_password&email=%@", kBaseUrl, email] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)contactUsWithName:(NSString *)name message:(NSString *)message email:(NSString *)email
{
    method = API_CONTACT_US;
    NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@send_contactform&contact_name=%@&contact_message=%@&contact_from=%@", kBaseUrl, name, message, email] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)sendInquiry:(NSString *)ids email:(NSString *)email name:(NSString *)name subject:(NSString *)subject message:(NSString *)message
{
    method = API_SEND_INQUIRY;
    NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@inquiry_to_all&profile_list=%@&email=%@&name=%@&subject=%@&message=%@", kBaseUrl, ids, email, name, subject, message] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)getUserDetail:(NSString *)userCookie
{
    method = API_GET_MYDETAIL;
    //NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://iweddingdirectory.com.au/iwed/api/custom/get_user_details/?dev=1&cookie=%@", userCookie]];
    NSURL *url = [NSURL URLWithString:[[@"http://iweddingdirectory.com.au/iwed/api/custom/get_user_details/?dev=1&cookie=" stringByAppendingFormat:@"%@",userCookie] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"cookie = %@", userCookie);
    NSLog(@"url = %@", url);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}

- (void)getUserInbox:(NSString *)userCookie
{
    method = API_GET_INBOX;
    NSURL *url = [NSURL URLWithString:[[@"http://iweddingdirectory.com.au/iwed/api/custom/get_user_details/?dev=1&cookie=" stringByAppendingFormat:@"%@",userCookie] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"cookie = %@", userCookie);
    NSLog(@"url = %@", url);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
    [connection start];
}



#pragma mark - NSURLConnectionDelegate

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@", error);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if (receivedData == nil) {
        receivedData = [[NSMutableData dataWithLength:0] retain];
    }
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    NSString *jsonString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSError *error = nil;
    NSDictionary *dictionary = [parser objectWithString:jsonString error:&error];
    if (error != nil) {
        NSLog(@"json: %@", jsonString);
        NSLog(@"error: %@", error);
    }
    [jsonString release];
    [parser release];
    if (method == API_GET_CATEGORIES) {
        [notificationCenter postNotificationName:kCategoriesReceivedNotification object:nil userInfo:dictionary];
    }
    else if (method == API_GET_SUB_CATEGORIES) {
        [notificationCenter postNotificationName:kSubCategoriesReceivedNotification object:nil userInfo:dictionary];
    }
    else if (method == API_GET_POSTS) {
        [notificationCenter postNotificationName:kPostsReceivedNotification object:nil userInfo:dictionary];
    }
    else if (method == API_GET_POST) {
        [notificationCenter postNotificationName:kPostReceivedNotification object:nil userInfo:dictionary];
    }
    else if (method == API_SEARCH) {
        [notificationCenter postNotificationName:kResultReceivedNotification object:nil userInfo:dictionary];
    }
    else if (method == API_LOGIN) {
        [notificationCenter postNotificationName:kLoginNotification object:nil userInfo:dictionary];
    }
    else if (method == API_REGISTER) {
        [notificationCenter postNotificationName:kRegisterNotification object:nil userInfo:dictionary];
    }
    else if (method == API_FORGOT_PASSWORD) {
        [notificationCenter postNotificationName:kForgotPasswordNotification object:nil userInfo:dictionary];
    }
    else if (method == API_CONTACT_US) {
        [notificationCenter postNotificationName:kContactUsNotification object:nil userInfo:dictionary];
    }
    else if (method == API_SEND_INQUIRY) {
        [notificationCenter postNotificationName:kSendInquiryNotification object:nil userInfo:dictionary];
    }
    else if (method == API_GET_MYDETAIL) {
        [notificationCenter postNotificationName:kMyDetailNotification object:nil userInfo:dictionary];
    }
    else if (method == API_GET_INBOX) {
        [notificationCenter postNotificationName:kInboxNotification object:nil userInfo:dictionary];
    }
}

@end
